package com.yd.app.mapper;

import com.yd.app.mapper.provider.MessageGroupProvider;
import com.yd.domain.searchParam.MessageGroupSearchParam;
import com.yd.domain.sys.MessageGroup;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Administrator on 2018/6/5.
 */
@Repository
public interface MessageGroupMapper {

    @Insert("INSERT INTO business_message_group (id,title,content,inputUser,inputDate,`state`,imgUrl) VALUES (#{messageGroup.id},#{messageGroup.title},#{messageGroup.content},#{messageGroup.inputUser},#{messageGroup.inputDate},#{messageGroup.state},#{messageGroup.imgUrl})")
    void addMessageGroup(@Param("messageGroup") MessageGroup messageGroup);

    @Select("SELECT count(*) FROM business_message_group")
    int getMessageGroupListCount(@Param("messageGroup") MessageGroupSearchParam param);

    @SelectProvider(method = "getMessageGroupList",type= MessageGroupProvider.class)
    List<MessageGroup> getMessageGroupList(@Param("model")MessageGroupSearchParam param, @Param("startRow") int startRow, @Param("pageSize") int pageSize);

    @Delete("DELETE from business_message_group WHERE id = #{messageGroupId}")
    void deleteMessageGroupById(@Param("messageGroupId")String messageGroupId);
    @Delete("DELETE from business_message_group_user WHERE messageGroupId = #{messageGroupId}")
    void deleteMessageGroupRelationById(@Param("messageGroupId")String messageGroupId);

    @Update("UPDATE business_message_group SET title = #{messageGroup.title},content = #{messageGroup.content},inputUser = #{messageGroup.inputUser},inputDate = #{messageGroup.inputDate},state = #{messageGroup.state},updateUser = #{messageGroup.updateUser},updateDate = #{messageGroup.updateDate},imgUrl = #{messageGroup.imgUrl} WHERE id = #{messageGroup.id}")
    void updateMessageGroup(@Param("messageGroup") MessageGroup messageGroup);

    @Select("SELECT bmgu.userId AS user,(SELECT realName FROM sys_user WHERE id = bmgu.userId) AS userName,bmgu.state AS userState,bmgu.isShow AS userShow FROM business_message_group_user bmgu WHERE bmgu.messageGroupId = #{messageGroupId}")
    List<MessageGroup> getMessageGroupUserList(@Param("messageGroupId") String messageGroupId);

    @Insert("INSERT INTO business_message_group_user (messageGroupId,userId,state,isShow) VALUES(#{messageGroup.id},#{messageGroup.user},#{messageGroup.userState},#{messageGroup.userShow})")
    void addMessageGroupUser(@Param("messageGroup") MessageGroup messageGroup);

    @Select("SELECT id FROM sys_user")
    List<String> getUserList();
    //查询关联表--没被客户删除的、未读的数量
    @Select("SELECT count(*) FROM business_message_group_user where  userId=#{userId} and state='0' and isShow='0'")
    int getNoReadQF(@Param("userId")String userId);
   //手机端---分页查询
    @SelectProvider(method = "getCusQFMsgList",type= MessageGroupProvider.class)
    List<MessageGroup> getCusQFMsgList(@Param("param")MessageGroupSearchParam param, @Param("startRow") int startRow, @Param("pageSize") int pageSize);
    @SelectProvider(method = "getCusQFMsgCount",type= MessageGroupProvider.class)
    int getCusQFMsgCount(@Param("param")MessageGroupSearchParam param);
    @Update("UPDATE business_message_group_user SET isShow = '1' WHERE messageGroupId =#{id} and userId=#{userId}")
    void deleteQf(@Param("id")String id,@Param("userId")String userId);
    @Update("UPDATE business_message_group_user SET state = '1' WHERE messageGroupId =#{id} and userId=#{userId}")
    void updateRead(@Param("id")String id, @Param("userId")String userId);
}
